﻿#include "OrderFile.h"

// 构造函数
OrderFile::OrderFile()
{
	this->init();// 初始化（从文件中读）
}

// 析构函数
OrderFile::~OrderFile()
{
	this->updateOrderData();// 更新预约数据(向文件中写）
}

// 更新预约数据(向文件中写）
void OrderFile::updateOrderData()
{
	if (this->m_Size == 0)
	{
		return;
	}
	ofstream ofs;
	ofs.open(ORDER_FILE, ios::out | ios::trunc);
	for (map<int, Order>::iterator it = this->m_mOrderData.begin(); it != this->m_mOrderData.end(); it++)
	{
		ofs << "stuId:" << it->second.m_StuId
			<< " stuName:" << it->second.m_StuName
			<< " date:" << it->second.m_Date
			<< " interval:" << it->second.m_Interval
			<< " roomId:" << it->second.m_RoomId
			<< " status:" << it->second.m_Status << endl;
	}
	ofs.close();
}

// 通过id寻找预约信息
void OrderFile::getOrderById(int id, map<int, Order>& m)
{
	for (map<int, Order>::iterator it = this->m_mOrderData.begin(); it != this->m_mOrderData.end(); it++)
	{
		if (it->second.m_StuId == id)
		{
			m.insert(make_pair(it->first, it->second));
		}
	}
}

// 展示所有的预约信息
void OrderFile::showAllOrder()
{
	if (this->m_Size == 0)
	{
		cout << "无预约信息！" << endl;
		return;
	}
	for (map<int, Order>::iterator it = this->m_mOrderData.begin(); it != this->m_mOrderData.end(); it++)
	{
		cout << "第" << it->first + 1 << "条预约信息：" << endl;
		cout << "学号：" << it->second.m_StuId << " "
			<< "用户名：" << it->second.m_StuName << " "
			<< "时间：" << DATE[it->second.m_Date]
			<< INTERVAL[it->second.m_Interval] << " "
			<< "地点：" << it->second.m_RoomId << "号机房 "
			<< "状态：" << STATUS[it->second.m_Status] << endl;
		cout << endl;
	}
}

// 根据":"截取后面信息
string subString(string s)
{
	int pos = s.find(":");
	return s.substr(pos + 1, s.size() - pos);
}

// 初始化（从文件中读）
void OrderFile::init()
{
	this->m_Size = 0;
	this->m_mOrderData.clear();

	ifstream ifs;
	ifs.open(ORDER_FILE, ios::in);
	if (!ifs.is_open())
	{
		cout << "预约信息文件不存在！" << endl;
		ifs.close();
		return;
	}
	string fId;
	string fName;
	string fDate;
	string fInterval;
	string fRoomId;
	string fStatus;
	while (ifs >> fId && ifs >> fName && ifs >> fDate && ifs >> fInterval && ifs >> fRoomId && ifs >> fStatus)
	{
		int stuId = atoi(subString(fId).c_str());
		string stuName = subString(fName);
		int date = atoi(subString(fDate).c_str());
		int interval = atoi(subString(fInterval).c_str());
		int roomId = atoi(subString(fRoomId).c_str());
		int status = atoi(subString(fStatus).c_str());
		Order o(stuId, stuName, date, interval, roomId, status);
		this->m_mOrderData.insert(make_pair(this->m_Size, o));
		this->m_Size++;
	}
}

// 取消某条预约
void OrderFile::cancelOrder(int index)
{
	this->m_mOrderData[index].m_Status = ORDER_CANCEL;

	//this->updateOrderData();
	//this->init();
}

// 添加一条预约
void OrderFile::addOrder(const Order& order)
{
	// 添加一条预约实际就是写入一条数据到文件中
	ofstream ofs;
	ofs.open(ORDER_FILE, ios::out | ios::app);
	ofs << "stuId:" << order.m_StuId
		<< " stuName:" << order.m_StuName
		<< " date:" << order.m_Date
		<< " interval:" << order.m_Interval
		<< " roomId:" << order.m_RoomId
		<< " status:" << order.m_Status << endl;
	ofs.close();
	// 更新数据
	//this->init();
	this->m_mOrderData.insert(make_pair(this->m_Size++, order));
}

// 获得所有需要审核的预约信息
void OrderFile::getReviewOrder(map<int, Order>& m)
{
	if (this->m_Size == 0)
	{
		return;
	}
	for (map<int, Order>::iterator it = this->m_mOrderData.begin(); it != this->m_mOrderData.end(); it++)
	{
		if (it->second.m_Status == ORDERING)
		{
			m.insert(make_pair(it->first, it->second));
		}
	}
}

// 审核一条预约
void OrderFile::review(int index, bool isPassed)
{
	if (isPassed)
	{
		this->m_mOrderData[index].m_Status = ORDERED;
	}
	else
	{
		this->m_mOrderData[index].m_Status = ORDER_REFUSED;
	}

	//this->updateOrderData();
	//this->init();
}